package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.RankItem;
import com.elinkway.infinitemovies.bean.RankList;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class RankDetailGridAdapter extends MyBaseAdapter {

	private RankList rankList;

	private class GridItemHolder {
		ImageView poster;
		TextView title;
		TextView intro;
		RelativeLayout rl_poster;
		TextView medal;
	}

	public RankDetailGridAdapter(Context context,
			RankList data) {
		super(context);
		rankList=data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RankItem video = rankList.getRankList().get(position);
		GridItemHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.grid_item_poster_small,
					null);
			holder = new GridItemHolder();

			holder.poster = (ImageView) convertView
					.findViewById(R.id.grid_item_poster);
			holder.title = (TextView) convertView
					.findViewById(R.id.grid_item_title);
			holder.intro = (TextView) convertView
					.findViewById(R.id.grid_item_intro);
			holder.rl_poster = (RelativeLayout) convertView
					.findViewById(R.id.poster_grid_item);
			holder.intro.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);
			convertView.setTag(holder);

		} else {
			holder = (GridItemHolder) convertView.getTag();
			if(holder.medal!=null){
				holder.rl_poster.removeView(holder.medal);
				holder.medal=new TextView(mContext);
			}
		}
		holder.title.setText(video.getName());
		if(!TextUtils.isEmpty(video.getRating())){
			holder.intro.setText(video.getRating() + "分");
			holder.intro.setVisibility(View.VISIBLE);
		}else{
			holder.intro.setVisibility(View.GONE);
		}
		ImageLoader.getInstance().displayImage(video.getPic_m_horse(), holder.poster);
//		 item.poster.setImageResource((Integer) map.get("poster"));
		if(position<3) {
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.ALIGN_PARENT_TOP
					| RelativeLayout.ALIGN_PARENT_RIGHT);
			holder.medal = initMedal(position + 1);
			holder.rl_poster.addView(holder.medal, lp);
		}
		return convertView;
	}

	private TextView initMedal(int position) {
		TextView medal = new TextView(mContext);
		medal.setGravity(Gravity.CENTER);
		medal.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		medal.setText(Integer.toString(position));
		medal.setBackgroundResource(R.drawable.rank_medal);
		medal.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
		medal.setTextColor(mContext.getResources().getColor(
				R.color.white_FFFFFF));
		return medal;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return rankList.getRankList().size();
	}
}
